﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UTC_Project_Manager.Model.Infrastructure;
using System.Net.Mail;
using System.Net;

namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for CurrentMeetingControl.xaml
    /// </summary>
    public partial class CurrentMeetingControl : UserControl
    {

        public delegate void EndOfMeetingEvent(Meeting meeting);
        public static event EndOfMeetingEvent endOfMeetingEvent;

        public delegate void NewReunionEvent(Meeting meeting);
        public static event NewReunionEvent newReunionCreatedEvent;

        public List<Member> members ;

        public CurrentMeetingControl()
        {
            InitializeComponent();
            members = new List<Member>();
            PopulateMembers();
        }

        public Meeting CurrentMeeting { get; set; }

        private void stopButton_Click(object sender, RoutedEventArgs e)
        {
            
            CurrentMeeting.Attended = members;
            CurrentMeeting.Notes = this.notesTextBox.Text;
            CurrentMeeting.Length = ((TimeSpan)(DateTime.Now - CurrentMeeting.Date)).ToString();
            CurrentMeeting.IsOver = true;
            //If we are editing the event, we need to remove the current event and replace it
            App.MyConfig.OurProject.Meetings.Add(CurrentMeeting);
            // Send the event
            newReunionCreatedEvent(CurrentMeeting);
            SendMail();
            endOfMeetingEvent(CurrentMeeting);
        }

        private void SendMail()
        {
            MailSettings settings = App.MyConfig.OurProject.Smtp;
            String sender = App.MyConfig.OurProject.Email;
            if (sender != null && settings != null)
            {
                foreach (Member member in App.MyConfig.OurProject.Members)
                {
                    MailMessage mail = new MailMessage(sender, member.Email);
                    mail.Subject = "[UTC ProjectManager] Compte Rendu de la réunion du" + CurrentMeeting.ToString();
                    mail.Body = "+Title: " +CurrentMeeting.Title + "\r\n";
                    mail.Body += "+Date: " + CurrentMeeting.ToString() + "\r\n";
                    mail.Body += "+Durée: " + CurrentMeeting.Length + "\r\n";
                    mail.Body += "+Description: \r\n\t" + CurrentMeeting.Description + "\r\n";
                    mail.Body += "+Agenda: \r\n\t" + CurrentMeeting.Points + "\r\n";
                    mail.Body += "+Notes: \r\n\t" + CurrentMeeting.Notes + "\r\n";
                    mail.Body += "+Membres invités: " + "\r\n" + getInvited(CurrentMeeting) +"\r\n";
                    mail.Body += "+Membres présents: " + "\r\n" + getAttended(CurrentMeeting) + "\r\n";

                    SmtpClient client = new SmtpClient(settings.Smtp, settings.Port);
                    if (settings.IsAnonymous)
                        client.Credentials = CredentialCache.DefaultNetworkCredentials;
                    else
                        client.Credentials = new NetworkCredential(settings.Login, settings.Password);
                    try
                    {
                        client.Send(mail);
                    }
                    catch (Exception ex) { }
                }

            }
        }


        public void PopulateMembers()
        {
            foreach (Member member in App.MyConfig.OurProject.Members)
            //foreach (Member member in formerMeeting)
            {
                CheckBox cb = new CheckBox();
                cb.IsChecked = false;
                cb.Checked += new RoutedEventHandler(cb_Checked);
                cb.Unchecked += new RoutedEventHandler(cb_Unchecked);
                cb.VerticalAlignment = VerticalAlignment.Center;
                cb.Name = member.ToString().Replace(" ", "");
                Label lab = new Label();
                lab.VerticalAlignment = VerticalAlignment.Center;
                lab.FontSize = 14;
                lab.Content = member.ToString();
                StackPanel spanel = new StackPanel();
                spanel.Orientation = Orientation.Horizontal;
                spanel.Children.Add(cb);
                spanel.Children.Add(lab);
                ListBoxItem lbi = new ListBoxItem();
                lbi.Content = spanel;
               this.membersListBox.Items.Add(lbi);

            }

        }

        void cb_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox currentCB = sender as CheckBox;
            foreach (Member member in App.MyConfig.OurProject.Members)
                if (currentCB.Name.Equals(member.ToString().Replace(" ", "")))
                    members.Remove(member);
        }

        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox currentCB = sender as CheckBox;
            foreach (Member member in App.MyConfig.OurProject.Members)
                if (currentCB.Name.Equals(member.ToString().Replace(" ", "")))
                    members.Add(member);
        }
        private string getAttended(Meeting meeting)
        {
            String st = "";
            foreach (Member m in meeting.Attended)
                st += "\t-" + m.ToString() +"\r\n";
            return st;


        }

        private string getInvited(Meeting meeting)
        {
            String st = "";
            foreach (Member m in meeting.Invited)
                st += "\t-" + m.ToString() +"\r\n";
            return st;


        }
    }
}
